# -*- coding: utf-8 -*-
# @Time : 2022-01-19 11:20
# @Author : Z
# @Function :
import os

import pytest

from Common.Excel.excel import get_reader, get_writer
from Common.Logger import logger
from Keywords.inter_keys import Inter


class Conf:

    def __init__(self):
        # 保存一个模块里面所有的用例
        self.cases = []
        # 初始化关键字对象
        self.obj = None
        # 设置分组信息
        self.feature = ''
        self.feature_idx = 0
        self.story = ''
        self.story_idx = 0
        # 记录运行的sheet
        self.s_idx = 1
        # 记录运行的用例的序号
        self.c_idx = 1
        self.writer = None
        self.case_type = 'inter'
        # 记录用例的序号
        self.case_idx = 0

    def runInterCase(self, case_file='', res_file=''):
        reader = get_reader(case_file)
        self.writer = get_writer(case_file, res_file)
        sheetname = reader.get_sheets()
        self.obj = Inter()

        self.s_idx = 1
        self.c_idx = 1
        try:
            for sheet in sheetname:
                reader.set_sheet(sheet)
                self.writer.set_sheet(sheet)
                self.feature = sheet
                self.feature_idx += 1
                self.story_idx = 0
                lines = reader.readline()
                case = []
                for i in range(1, len(lines)):
                    line = lines[i]
                    if len(line[0]) > 1:
                        if case:
                            self.cases.append(case)
                            # 执行
                            if i == 1:
                                self.__run_pytest(1)
                            else:
                                self.__run_pytest()
                            case = []
                        self.story = line[0]
                        self.story_idx += 1

                        # 用例组序号加1
                        self.case_idx += 1

                        self.writer.row = i

                    elif len(line[1]) > 1:
                        if case:
                            self.cases.append(case)
                        case = []
                        case.append(line)

                    else:
                        case.append(line)

                if case:
                    self.cases.append(case)

                if self.cases:
                    # 执行
                    self.__run_pytest()

                self.s_idx += 1

            self.writer.save_close()
        except Exception as e:
            pass
        finally:
            # 全部用例组执行完成后，把用例文件名字改回test_inter_0.py。防止下一次执行找不到文件。
            os.rename('./ddt/test_%s_%d.py' % (self.case_type, self.case_idx), './ddt/test_inter_0.py')

    def __run_pytest(self, is_start=0):
        # logger.info('执行')
        logger.info(self.cases)
        # 跑之前，我们把pytest执行文件，改成test_inter_idx.py
        os.rename('./ddt/test_%s_%d.py' % (self.case_type, self.case_idx - 1),
                  './ddt/test_%s_%d.py' % (self.case_type, self.case_idx))
        pytest.main(['-s', './ddt/test_%s_%d.py' % (self.case_type, self.case_idx), '--alluredir', './result'])
        self.c_idx += 1
        self.cases.clear()


conf = Conf()
